package promotion

import (
	// "bp/cache"
	sqlc "bp/db/sqlc"
	"bp/internal/service/param"
	"context"
)

// ListItemCategoryConfigurationPtrsByPromotionID 获取优惠券到商品品类配置数组
func (s *PromotionEntityServiceImp) ListItemCategoryConfigurationPtrsByPromotionID(ctx context.Context, q *sqlc.Queries, couponID int64) (arr param.ItemCategoryConfigurationPtrArray, err error) {
	couponItemCategories, err := q.ListNormalPromotionItemCategoriesByPromotionID(ctx, couponID)
	if err != nil {
		return
	}
	for _, couponItemCategory := range couponItemCategories {
		arr = append(arr, &param.ItemCategoryConfiguration{
			ID:   couponItemCategory.ItemCategoryID,
			Name: couponItemCategory.ItemCategoryName,
		})
	}
	return
}

// GetMapOfPromotionIDToItemCategoryConfigurationPtrsByPromotionIDs 获取促销活动到商品品类配置的映射
func (s *PromotionEntityServiceImp) GetMapOfPromotionIDToItemCategoryConfigurationPtrsByPromotionIDs(ctx context.Context, q *sqlc.Queries, promotionIDs []int64) (m param.EntityIDToItemCategoryConfigurationPtrsMap, err error) {
	m = make(param.EntityIDToItemCategoryConfigurationPtrsMap)
	if len(promotionIDs) == 0 {
		return
	}
	promotionItemCategories, err := q.ListNormalPromotionItemCategoriesByPromotionIDs(ctx, promotionIDs)
	if err != nil {
		return
	}
	for _, promotionItemCategory := range promotionItemCategories {
		promotionID := promotionItemCategory.PromotionID
		valPtr := &param.ItemCategoryConfiguration{
			ID:   promotionItemCategory.ItemCategoryID,
			Name: promotionItemCategory.ItemCategoryName,
		}
		if val, ok := m[promotionID]; ok {
			val = append(val, valPtr)
			m[promotionID] = val
		} else {
			m[promotionID] = []*param.ItemCategoryConfiguration{valPtr}
		}
	}
	return
}

// GetMapOfPromotionIDToItemCategoryConfigurationPtrsByPromotionPtrs 获取促销活动到商品品类配置的映射
func (s *PromotionEntityServiceImp) GetMapOfPromotionIDToItemCategoryConfigurationPtrsByPromotionPtrs(ctx context.Context, q *sqlc.Queries, ptrs []*sqlc.Promotion) (m param.EntityIDToItemCategoryConfigurationPtrsMap, err error) {
	return s.GetMapOfPromotionIDToItemCategoryConfigurationPtrsByPromotionIDs(ctx, q, s.ConvToPromotionIDs(ptrs))
}

// GetMapOfPromotionIDToItemCategoryConfigurationPtrsByListPromotionWithStatRowPtrs 获取促销活动到商品品类配置的映射
func (s *PromotionEntityServiceImp) GetMapOfPromotionIDToItemCategoryConfigurationPtrsByListPromotionWithStatRowPtrs(ctx context.Context, q *sqlc.Queries, ptrs []*sqlc.ListPromotionWithStatsRow) (m param.EntityIDToItemCategoryConfigurationPtrsMap, err error) {
	return s.GetMapOfPromotionIDToItemCategoryConfigurationPtrsByPromotionIDs(ctx, q, s.ConvToPromotionIDs(ptrs))
}